<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="学生学号" prop="sno">
      <el-input v-model="dataForm.sno" placeholder="学生学号"></el-input>
    </el-form-item>
    <el-form-item label="学生姓名" prop="name">
      <el-input v-model="dataForm.name" placeholder="学生姓名"></el-input>
    </el-form-item>
    <el-form-item label="学生性别    f-女    m-男" prop="gender">
      <el-input v-model="dataForm.gender" placeholder="学生性别    f-女    m-男"></el-input>
    </el-form-item>
    <el-form-item label="民族" prop="nation">
      <el-input v-model="dataForm.nation" placeholder="民族"></el-input>
    </el-form-item>
    <el-form-item label="专业" prop="major">
      <el-input v-model="dataForm.major" placeholder="专业"></el-input>
    </el-form-item>
    <el-form-item label="班级" prop="stuClass">
      <el-input v-model="dataForm.stuClass" placeholder="班级"></el-input>
    </el-form-item>
    <el-form-item label="联系方式" prop="phone">
      <el-input v-model="dataForm.phone" placeholder="联系方式"></el-input>
    </el-form-item>
    <el-form-item label="公寓id" prop="apartmentId">
      <el-input v-model="dataForm.apartmentId" placeholder="公寓id"></el-input>
    </el-form-item>
    <el-form-item label="寝室id" prop="dormitoryId">
      <el-input v-model="dataForm.dormitoryId" placeholder="寝室id"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          sno: '',
          name: '',
          gender: '',
          nation: '',
          major: '',
          stuClass: '',
          phone: '',
          apartmentId: '',
          dormitoryId: '',
          createTime: '',
          updateTime: ''
        },
        dataRule: {
          sno: [
            { required: true, message: '学生学号不能为空', trigger: 'blur' }
          ],
          name: [
            { required: true, message: '学生姓名不能为空', trigger: 'blur' }
          ],
          gender: [
            { required: true, message: '学生性别    f-女    m-男不能为空', trigger: 'blur' }
          ],
          nation: [
            { required: true, message: '民族不能为空', trigger: 'blur' }
          ],
          major: [
            { required: true, message: '专业不能为空', trigger: 'blur' }
          ],
          stuClass: [
            { required: true, message: '班级不能为空', trigger: 'blur' }
          ],
          phone: [
            { required: true, message: '联系方式不能为空', trigger: 'blur' }
          ],
          apartmentId: [
            { required: true, message: '公寓id不能为空', trigger: 'blur' }
          ],
          dormitoryId: [
            { required: true, message: '寝室id不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/apartment/stuinfo/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.sno = data.stuInfo.sno
                this.dataForm.name = data.stuInfo.name
                this.dataForm.gender = data.stuInfo.gender
                this.dataForm.nation = data.stuInfo.nation
                this.dataForm.major = data.stuInfo.major
                this.dataForm.stuClass = data.stuInfo.stuClass
                this.dataForm.phone = data.stuInfo.phone
                this.dataForm.apartmentId = data.stuInfo.apartmentId
                this.dataForm.dormitoryId = data.stuInfo.dormitoryId
                this.dataForm.createTime = data.stuInfo.createTime
                this.dataForm.updateTime = data.stuInfo.updateTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/apartment/stuinfo/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'sno': this.dataForm.sno,
                'name': this.dataForm.name,
                'gender': this.dataForm.gender,
                'nation': this.dataForm.nation,
                'major': this.dataForm.major,
                'stuClass': this.dataForm.stuClass,
                'phone': this.dataForm.phone,
                'apartmentId': this.dataForm.apartmentId,
                'dormitoryId': this.dataForm.dormitoryId,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
